<template>
	<view class="my_address">
		<view v-if="Object.keys(addressData).length === 0" class="address_choose__box">
		  <button type="primary" size="mini" class="btnChooseAddress" @click="chooseAddress">请选择收货地址+</button>
		</view>
		<view v-else class="address_info__box"  @click="chooseAddress">
		  <view class="row1">
		    <view class="row1_left">
		      <view class="username">收货人：<text>{{addressData.userName}}</text></view>
		    </view>
		    <view class="row1_right">
		      <view class="phone">电话：<text>{{addressData.telNumber}}</text></view>
		      <uni-icons type="arrowright" size="16"></uni-icons>
		    </view>
		  </view>
		  <view class="row2">
		    <view class="row2_left">收货地址：</view>
		    <view class="row2_right">{{addressDataStr}}</view>
		  </view>
			<image class="address_border" src="../../static/cart_border@2x.png"></image>
		</view>
		
	</view>
</template>

<script>
import { mapState, mapMutations, mapGetters} from 'vuex'
import { showMsg } from '../../static/common'
	export default {
		name:"my-address",
		data() {
			return {
				// addressData: {},
			};
		},
		computed: {
			...mapState('my_user', ['addressData']),
			...mapGetters('my_user', ['addressDataStr']),
		},
		
		methods: {
			...mapMutations('my_user', ['updataAddressData']),
			async chooseAddress() {
				//调用小程序提供的chooseAddress()，即可使用选择收货地址的功能
				const [erroer, success] = await uni.chooseAddress().then().catch((erroer) => erroer);
				console.log('erroer', erroer, 'success', success);
				if(erroer === null && success.errMsg === 'chooseAddress:ok') this.addressData = success;
				this.updataAddressData(success);
				if(erroer) {
					//用户没有授权
					if (erroer && erroer.errMsg === 'chooseAddress:fail auth deny' || erroer.errMsg === 'chooseAddress:fail authorize no response')  this.reAuth(); // 调用 this.reAuth() 方法，向用户重新发起授权申请
					}
				},
				
			
			// 调用此方法，重新发起收货地址的授权
			async reAuth() {
			  //提示用户对地址进行授权
			  const [err2, confirmResult] = await uni.showModal({
			    content: '检测到您没打开地址权限，是否去设置打开？',
			    confirmText: "确认",
			    cancelText: "取消",
			  })
			
			  // 如果弹框异常，则直接退出
			  if (err2) return;
			  if (confirmResult.cancel) return showMsg.showErrMsg('您取消了地址授权！');
			  if (confirmResult.confirm) return uni.openSetting({
			    // 授权结束，需要对授权的结果做进一步判断
			    success: (settingResult) => {
			      if (settingResult.authSetting['scope.addressData']) return showMsg.showErrMsg('授权成功！请选择地址');
			      if (!settingResult.authSetting['scope.addressData']) return showMsg.showErrMsg('您取消了地址授权！');
			    },
			  });
			},
		},
	}
</script>

<style lang="scss">
.my_address {
	.address_choose__box {
		height: 180rpx;
		display: flex;
		justify-content: center;
		align-items: center;
	}
	.address_info__box {
		height: 160rpx;
		display: flex;
		flex-direction: column;
		justify-content: center;
		padding: 0 15rpx;
		.row1 {
			display: flex;
			justify-content: space-between;
			.row1_left { }
			.row1_right {
				display: flex;
			}
		}
		.row2 {
			display: flex;
			align-items: center;
			margin-top: 15rpx;
			.row2_left {
				min-width: 200rpx;
			}
			.row2_right {
				white-space: nowrap;
				overflow-x: hidden;
				text-overflow: ellipsis;
			}
		}
	}
	.address_border {
		display: block;
		width: 100%;
		height: 10rpx;
	}
}
</style>